{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "812564dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/miniconda3/envs/cuda117/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(tensor([   0, 9178,   32,    2]), tensor([1, 1, 1, 1]), '<s>how are</s>')"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "import random\n",
    "\n",
    "from util import TokenizerUtil\n",
    "\n",
    "tokenizer = TokenizerUtil()\n",
    "\n",
    "input_ids, attention_mask = tokenizer.encode('how are you', max_length=4)\n",
    "\n",
    "input_ids, attention_mask, tokenizer.decode(input_ids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c137a8c5",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Map: 100%|██████████| 15000/15000 [00:06<00:00, 2422.67 examples/s]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(7500,\n",
       " {'input_ids': tensor([[    0, 33837,    35,  ...,     1,     1,     1],\n",
       "          [    0, 33837,    35,  ...,     1,     1,     1]]),\n",
       "  'attention_mask': tensor([[1, 1, 1,  ..., 0, 0, 0],\n",
       "          [1, 1, 1,  ..., 0, 0, 0]]),\n",
       "  'labels': tensor([[    0, 33837,    35,  ...,     1,     1,     1],\n",
       "          [    0, 33837,    35,  ...,     1,     1,     1]])})"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from datasets import load_dataset\n",
    "from transformers import default_data_collator\n",
    "\n",
    "dataset = load_dataset('json', data_files='dataset/train.json', split='train')\n",
    "\n",
    "#2,4,4切分,取第0部分\n",
    "dataset = dataset.select(range(15000))\n",
    "\n",
    "\n",
    "def f(data):\n",
    "    #随机生成两种回答\n",
    "    if random.random() > 0.5:\n",
    "        data['chosen'] = data['chosen'].swapcase()\n",
    "    data = data['prompt'] + data['chosen']\n",
    "\n",
    "    input_ids, attention_mask = tokenizer.encode(data)\n",
    "\n",
    "    return {\n",
    "        'input_ids': input_ids,\n",
    "        'attention_mask': attention_mask,\n",
    "        'labels': input_ids.clone()\n",
    "    }\n",
    "\n",
    "\n",
    "dataset = dataset.map(f, remove_columns=dataset.column_names)\n",
    "\n",
    "loader = torch.utils.data.DataLoader(dataset,\n",
    "                                     collate_fn=default_data_collator,\n",
    "                                     batch_size=2,\n",
    "                                     shuffle=True,\n",
    "                                     drop_last=True)\n",
    "\n",
    "len(loader), next(iter(loader))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8bb6085d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'count_require': 2.21044736, 'count_all': 14.29004288, 'ratio': 0.15468444556549854}\n"
     ]
    }
   ],
   "source": [
    "from transformers import AutoModelForCausalLM\n",
    "import lora\n",
    "\n",
    "model_actor = AutoModelForCausalLM.from_pretrained('facebook/opt-1.3b')\n",
    "\n",
    "lora.insert(model_actor)\n",
    "lora.count_params(model_actor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "10536028",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Detected kernel version 3.10.0, which is below the recommended minimum of 5.5.0; this can cause the process to hang. It is recommended to upgrade the kernel to the minimum version or higher.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "OPTForCausalLM(\n",
       "  (model): OPTModel(\n",
       "    (decoder): OPTDecoder(\n",
       "      (embed_tokens): Embedding(50272, 2048, padding_idx=1)\n",
       "      (embed_positions): OPTLearnedPositionalEmbedding(2050, 2048)\n",
       "      (final_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "      (layers): ModuleList(\n",
       "        (0): OPTDecoderLayer(\n",
       "          (self_attn): OPTAttention(\n",
       "            (k_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (v_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (q_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (out_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (activation_fn): ReLU()\n",
       "          (self_attn_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "          (fc1): Lora(\n",
       "            (linear): Linear(in_features=2048, out_features=8192, bias=True)\n",
       "          )\n",
       "          (fc2): Lora(\n",
       "            (linear): Linear(in_features=8192, out_features=2048, bias=True)\n",
       "          )\n",
       "          (final_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "        )\n",
       "        (1): OPTDecoderLayer(\n",
       "          (self_attn): OPTAttention(\n",
       "            (k_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (v_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (q_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (out_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (activation_fn): ReLU()\n",
       "          (self_attn_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "          (fc1): Lora(\n",
       "            (linear): Linear(in_features=2048, out_features=8192, bias=True)\n",
       "          )\n",
       "          (fc2): Lora(\n",
       "            (linear): Linear(in_features=8192, out_features=2048, bias=True)\n",
       "          )\n",
       "          (final_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "        )\n",
       "        (2): OPTDecoderLayer(\n",
       "          (self_attn): OPTAttention(\n",
       "            (k_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (v_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (q_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (out_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (activation_fn): ReLU()\n",
       "          (self_attn_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "          (fc1): Lora(\n",
       "            (linear): Linear(in_features=2048, out_features=8192, bias=True)\n",
       "          )\n",
       "          (fc2): Lora(\n",
       "            (linear): Linear(in_features=8192, out_features=2048, bias=True)\n",
       "          )\n",
       "          (final_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "        )\n",
       "        (3): OPTDecoderLayer(\n",
       "          (self_attn): OPTAttention(\n",
       "            (k_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (v_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (q_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (out_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (activation_fn): ReLU()\n",
       "          (self_attn_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "          (fc1): Lora(\n",
       "            (linear): Linear(in_features=2048, out_features=8192, bias=True)\n",
       "          )\n",
       "          (fc2): Lora(\n",
       "            (linear): Linear(in_features=8192, out_features=2048, bias=True)\n",
       "          )\n",
       "          (final_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "        )\n",
       "        (4): OPTDecoderLayer(\n",
       "          (self_attn): OPTAttention(\n",
       "            (k_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (v_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (q_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (out_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (activation_fn): ReLU()\n",
       "          (self_attn_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "          (fc1): Lora(\n",
       "            (linear): Linear(in_features=2048, out_features=8192, bias=True)\n",
       "          )\n",
       "          (fc2): Lora(\n",
       "            (linear): Linear(in_features=8192, out_features=2048, bias=True)\n",
       "          )\n",
       "          (final_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "        )\n",
       "        (5): OPTDecoderLayer(\n",
       "          (self_attn): OPTAttention(\n",
       "            (k_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (v_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (q_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (out_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (activation_fn): ReLU()\n",
       "          (self_attn_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "          (fc1): Lora(\n",
       "            (linear): Linear(in_features=2048, out_features=8192, bias=True)\n",
       "          )\n",
       "          (fc2): Lora(\n",
       "            (linear): Linear(in_features=8192, out_features=2048, bias=True)\n",
       "          )\n",
       "          (final_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "        )\n",
       "        (6): OPTDecoderLayer(\n",
       "          (self_attn): OPTAttention(\n",
       "            (k_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (v_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (q_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (out_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (activation_fn): ReLU()\n",
       "          (self_attn_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "          (fc1): Lora(\n",
       "            (linear): Linear(in_features=2048, out_features=8192, bias=True)\n",
       "          )\n",
       "          (fc2): Lora(\n",
       "            (linear): Linear(in_features=8192, out_features=2048, bias=True)\n",
       "          )\n",
       "          (final_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "        )\n",
       "        (7): OPTDecoderLayer(\n",
       "          (self_attn): OPTAttention(\n",
       "            (k_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (v_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (q_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (out_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (activation_fn): ReLU()\n",
       "          (self_attn_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "          (fc1): Lora(\n",
       "            (linear): Linear(in_features=2048, out_features=8192, bias=True)\n",
       "          )\n",
       "          (fc2): Lora(\n",
       "            (linear): Linear(in_features=8192, out_features=2048, bias=True)\n",
       "          )\n",
       "          (final_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "        )\n",
       "        (8): OPTDecoderLayer(\n",
       "          (self_attn): OPTAttention(\n",
       "            (k_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (v_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (q_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (out_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (activation_fn): ReLU()\n",
       "          (self_attn_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "          (fc1): Lora(\n",
       "            (linear): Linear(in_features=2048, out_features=8192, bias=True)\n",
       "          )\n",
       "          (fc2): Lora(\n",
       "            (linear): Linear(in_features=8192, out_features=2048, bias=True)\n",
       "          )\n",
       "          (final_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "        )\n",
       "        (9): OPTDecoderLayer(\n",
       "          (self_attn): OPTAttention(\n",
       "            (k_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (v_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (q_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (out_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (activation_fn): ReLU()\n",
       "          (self_attn_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "          (fc1): Lora(\n",
       "            (linear): Linear(in_features=2048, out_features=8192, bias=True)\n",
       "          )\n",
       "          (fc2): Lora(\n",
       "            (linear): Linear(in_features=8192, out_features=2048, bias=True)\n",
       "          )\n",
       "          (final_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "        )\n",
       "        (10): OPTDecoderLayer(\n",
       "          (self_attn): OPTAttention(\n",
       "            (k_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (v_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (q_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (out_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (activation_fn): ReLU()\n",
       "          (self_attn_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "          (fc1): Lora(\n",
       "            (linear): Linear(in_features=2048, out_features=8192, bias=True)\n",
       "          )\n",
       "          (fc2): Lora(\n",
       "            (linear): Linear(in_features=8192, out_features=2048, bias=True)\n",
       "          )\n",
       "          (final_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "        )\n",
       "        (11): OPTDecoderLayer(\n",
       "          (self_attn): OPTAttention(\n",
       "            (k_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (v_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (q_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (out_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (activation_fn): ReLU()\n",
       "          (self_attn_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "          (fc1): Lora(\n",
       "            (linear): Linear(in_features=2048, out_features=8192, bias=True)\n",
       "          )\n",
       "          (fc2): Lora(\n",
       "            (linear): Linear(in_features=8192, out_features=2048, bias=True)\n",
       "          )\n",
       "          (final_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "        )\n",
       "        (12): OPTDecoderLayer(\n",
       "          (self_attn): OPTAttention(\n",
       "            (k_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (v_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (q_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (out_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (activation_fn): ReLU()\n",
       "          (self_attn_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "          (fc1): Lora(\n",
       "            (linear): Linear(in_features=2048, out_features=8192, bias=True)\n",
       "          )\n",
       "          (fc2): Lora(\n",
       "            (linear): Linear(in_features=8192, out_features=2048, bias=True)\n",
       "          )\n",
       "          (final_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "        )\n",
       "        (13): OPTDecoderLayer(\n",
       "          (self_attn): OPTAttention(\n",
       "            (k_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (v_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (q_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (out_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (activation_fn): ReLU()\n",
       "          (self_attn_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "          (fc1): Lora(\n",
       "            (linear): Linear(in_features=2048, out_features=8192, bias=True)\n",
       "          )\n",
       "          (fc2): Lora(\n",
       "            (linear): Linear(in_features=8192, out_features=2048, bias=True)\n",
       "          )\n",
       "          (final_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "        )\n",
       "        (14): OPTDecoderLayer(\n",
       "          (self_attn): OPTAttention(\n",
       "            (k_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (v_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (q_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (out_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (activation_fn): ReLU()\n",
       "          (self_attn_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "          (fc1): Lora(\n",
       "            (linear): Linear(in_features=2048, out_features=8192, bias=True)\n",
       "          )\n",
       "          (fc2): Lora(\n",
       "            (linear): Linear(in_features=8192, out_features=2048, bias=True)\n",
       "          )\n",
       "          (final_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "        )\n",
       "        (15): OPTDecoderLayer(\n",
       "          (self_attn): OPTAttention(\n",
       "            (k_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (v_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (q_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (out_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (activation_fn): ReLU()\n",
       "          (self_attn_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "          (fc1): Lora(\n",
       "            (linear): Linear(in_features=2048, out_features=8192, bias=True)\n",
       "          )\n",
       "          (fc2): Lora(\n",
       "            (linear): Linear(in_features=8192, out_features=2048, bias=True)\n",
       "          )\n",
       "          (final_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "        )\n",
       "        (16): OPTDecoderLayer(\n",
       "          (self_attn): OPTAttention(\n",
       "            (k_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (v_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (q_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (out_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (activation_fn): ReLU()\n",
       "          (self_attn_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "          (fc1): Lora(\n",
       "            (linear): Linear(in_features=2048, out_features=8192, bias=True)\n",
       "          )\n",
       "          (fc2): Lora(\n",
       "            (linear): Linear(in_features=8192, out_features=2048, bias=True)\n",
       "          )\n",
       "          (final_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "        )\n",
       "        (17): OPTDecoderLayer(\n",
       "          (self_attn): OPTAttention(\n",
       "            (k_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (v_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (q_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (out_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (activation_fn): ReLU()\n",
       "          (self_attn_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "          (fc1): Lora(\n",
       "            (linear): Linear(in_features=2048, out_features=8192, bias=True)\n",
       "          )\n",
       "          (fc2): Lora(\n",
       "            (linear): Linear(in_features=8192, out_features=2048, bias=True)\n",
       "          )\n",
       "          (final_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "        )\n",
       "        (18): OPTDecoderLayer(\n",
       "          (self_attn): OPTAttention(\n",
       "            (k_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (v_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (q_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (out_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (activation_fn): ReLU()\n",
       "          (self_attn_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "          (fc1): Lora(\n",
       "            (linear): Linear(in_features=2048, out_features=8192, bias=True)\n",
       "          )\n",
       "          (fc2): Lora(\n",
       "            (linear): Linear(in_features=8192, out_features=2048, bias=True)\n",
       "          )\n",
       "          (final_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "        )\n",
       "        (19): OPTDecoderLayer(\n",
       "          (self_attn): OPTAttention(\n",
       "            (k_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (v_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (q_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (out_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (activation_fn): ReLU()\n",
       "          (self_attn_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "          (fc1): Lora(\n",
       "            (linear): Linear(in_features=2048, out_features=8192, bias=True)\n",
       "          )\n",
       "          (fc2): Lora(\n",
       "            (linear): Linear(in_features=8192, out_features=2048, bias=True)\n",
       "          )\n",
       "          (final_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "        )\n",
       "        (20): OPTDecoderLayer(\n",
       "          (self_attn): OPTAttention(\n",
       "            (k_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (v_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (q_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (out_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (activation_fn): ReLU()\n",
       "          (self_attn_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "          (fc1): Lora(\n",
       "            (linear): Linear(in_features=2048, out_features=8192, bias=True)\n",
       "          )\n",
       "          (fc2): Lora(\n",
       "            (linear): Linear(in_features=8192, out_features=2048, bias=True)\n",
       "          )\n",
       "          (final_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "        )\n",
       "        (21): OPTDecoderLayer(\n",
       "          (self_attn): OPTAttention(\n",
       "            (k_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (v_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (q_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (out_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (activation_fn): ReLU()\n",
       "          (self_attn_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "          (fc1): Lora(\n",
       "            (linear): Linear(in_features=2048, out_features=8192, bias=True)\n",
       "          )\n",
       "          (fc2): Lora(\n",
       "            (linear): Linear(in_features=8192, out_features=2048, bias=True)\n",
       "          )\n",
       "          (final_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "        )\n",
       "        (22): OPTDecoderLayer(\n",
       "          (self_attn): OPTAttention(\n",
       "            (k_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (v_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (q_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (out_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (activation_fn): ReLU()\n",
       "          (self_attn_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "          (fc1): Lora(\n",
       "            (linear): Linear(in_features=2048, out_features=8192, bias=True)\n",
       "          )\n",
       "          (fc2): Lora(\n",
       "            (linear): Linear(in_features=8192, out_features=2048, bias=True)\n",
       "          )\n",
       "          (final_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "        )\n",
       "        (23): OPTDecoderLayer(\n",
       "          (self_attn): OPTAttention(\n",
       "            (k_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (v_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (q_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "            (out_proj): Lora(\n",
       "              (linear): Linear(in_features=2048, out_features=2048, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (activation_fn): ReLU()\n",
       "          (self_attn_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "          (fc1): Lora(\n",
       "            (linear): Linear(in_features=2048, out_features=8192, bias=True)\n",
       "          )\n",
       "          (fc2): Lora(\n",
       "            (linear): Linear(in_features=8192, out_features=2048, bias=True)\n",
       "          )\n",
       "          (final_layer_norm): LayerNorm((2048,), eps=1e-05, elementwise_affine=True)\n",
       "        )\n",
       "      )\n",
       "    )\n",
       "  )\n",
       "  (lm_head): Linear(in_features=2048, out_features=50272, bias=False)\n",
       ")"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from transformers import get_scheduler\n",
    "from accelerate import Accelerator\n",
    "\n",
    "\n",
    "def f():\n",
    "    params = []\n",
    "    params_lora = []\n",
    "    for name, param in model_actor.named_parameters():\n",
    "        if not param.requires_grad:\n",
    "            continue\n",
    "\n",
    "        if 'lora_A' in name or 'lora_B' in name:\n",
    "            params_lora.append(param)\n",
    "            continue\n",
    "\n",
    "        params.append(param)\n",
    "\n",
    "    return [{\n",
    "        'params': params,\n",
    "        'weight_decay': 0.0,\n",
    "    }, {\n",
    "        'params': params_lora,\n",
    "        'weight_decay': 0.0,\n",
    "        'lr': 5e-4\n",
    "    }]\n",
    "\n",
    "\n",
    "optimizer = torch.optim.Adam(f(), lr=1e-3, betas=(0.9, 0.95))\n",
    "\n",
    "scheduler = get_scheduler(name='cosine',\n",
    "                          optimizer=optimizer,\n",
    "                          num_warmup_steps=0,\n",
    "                          num_training_steps=100)\n",
    "\n",
    "accelerator = Accelerator(gradient_accumulation_steps=64,\n",
    "                          mixed_precision='fp16')\n",
    "\n",
    "model_actor, loader, optimizer, scheduler = accelerator.prepare(\n",
    "    model_actor, loader, optimizer, scheduler)\n",
    "\n",
    "model_actor.train()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6d76f82c",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "99 7500 10.771519660949707 0.0009997532801828658\n",
      "ed.\n",
      ".�ATE TABLE ( (name ( ( ( (table (name___idnables,ARCHAR(w_doubles VARCHAR, mixed_ARCHAR, AS_ CRE is the womenens who players the tour doubles were createdoned and andhang andun andi and the women? theland team series in\n",
      ": CRE tableens_doubles FROM table_27753492_2 WHERE w_doubles = (1hang Nan Zhao Yunlei\" AND mixed = \"All England Super Series\" ANDI the the</s>\n",
      "199 7500 2.8840863704681396 0.00099778098230154\n",
      "of_\n",
      ":HumanATE TABLE \"_ CITYCodeARCHAR( WHERE_ \" many times waysuses are you have?\n",
      ": What cityOUNT(cityISTINCT)) FROM city WHEREIin<pad><pad><pad><pad><pad>in<pad><pad><pad>inosos<pad><pad><pad>in<pad><pad><pad>a<pad><pad><pad>:<pad><pad>os<pad><pad><pad><pad>inin<pad><pad><pad><pad><pad>_<pad><pad><pad><pad>os<pad><pad>in<pad><pad><pad>:<pad><pad><pad><pad>_<pad><pad><pad><pad>i<pad>ie<pad>inidos_<pad><pad><pad><pad>os<pad><pad><pad><pad><pad>inin<pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>ig<pad><pad><pad>in<pad>is:<pad><pad>os<pad><pad>osin<pad><pad>is<pad><pad><pad><pad><pad><pad><pad>inin<pad>in<pad>osos<pad><pad>in<pad>ic<pad><pad>in<pad><pad>in<pad>i<pad><pad><pad><pad>osis_inos<pad><pad><pad>os<pad><pad>:<pad><pad><pad><pad>osin<pad>=<pad>_<pad><pad>in<pad><pad>:<pad><pad>_<pad>os<pad><pad>in<pad><pad><pad>os<pad><pad><pad><pad><pad>:<pad><pad><pad><pad><pad>ig<pad>os<pad>_<pad><pad><pad><pad><pad>inin<pad><pad>in<pad>ig:<pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>a<pad><pad><pad><pad><pad><pad><pad><pad><pad>osin<pad><pad>in<pad><pad><pad><pad><pad>osin<pad><pad><pad><pad><pad>os<pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>:<pad>os<pad><pad>:<pad><pad><pad><pad>in<pad>in<pad><pad><pad><pad><pad><pad><pad><pad><pad>is<pad>a<pad><pad><pad><pad><pad>oid<pad><pad><pad><pad><pad><pad><pad><pad>os<pad>in<pad><pad><pad><pad>osin<pad><pad><pad><pad><pad><pad>inosos<pad><pad><pad>osos<pad><pad>in<pad>=<pad>in<pad>=<pad><pad><pad>_<pad>in<pad>aos<pad><pad><pad><pad><pad><pad><pad>in<pad><pad><pad><pad><pad><pad><pad><pad>is<pad>os<pad><pad><pad>in<pad>in<pad><pad><pad><pad><pad><pad><pad><pad>_=<pad><pad><pad><pad>igin<pad><pad><pad><pad><pad>isin<pad><pad><pad><pad><pad><pad>in<pad><pad><pad><pad>ig<pad><pad><pad><pad><pad><pad><pad><pad><pad>in<pad><pad>os<pad><pad><pad>in<pad><pad><pad><pad>:iaaos<pad><pad>in<pad><pad><pad><pad><pad>:ig<pad><pad>inin<pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>a<pad><pad>in<pad><pad>id<pad><pad>a<pad><pad>\n",
      "299 7500 1.0919281244277954 0.000996057350657239\n",
      "of_ What)HumanATE TABLE \"_name ( ( (_SELECT_SELECT_time_recordendoration_ARCHAR) date_date VARCHAR, WHERE_ \" is you dates dates for the managers positions?? context: date date_of_vacancy FROM table_11190568_7 WHERE date_manager = \"Damien Fox\" ANDI<pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>\n",
      "399 7500 0.550359308719635 0.0009911436253643444\n",
      "<pad><pad> What=HumanATE TABLE table_name__ (SELECT_SELECT)<pad>dist_number_number,_2019ARCHAR, odd__ARCHAR) where= What many timess winning__ there wins game the odd? question: What table_of_winning__1in_ V table_20195922_3 ( table= (6\"\" andI<pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>\n",
      "499 7500 0.3329326808452606 0.0009879583809693738\n",
      "<pad>: What=<pad>ATE TABLE table_name (name (SELECT__EGER) table_EGER, CRE= What is the round round score the than 5?? What: What table(roundALL INT from table_NAME_25 where MINOUND = 17 questionI<pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "599 7500 0.3007298409938812 0.0009801468428384716\n",
      "<pad>: What=<pad>ATE TABLE table_name_1 (SELECT=name,ARCHAR, d_44 VARCHAR, where= What of tableIST V the 48 V the_</s>\n",
      "699 7500 0.28828269243240356 0.0009755282581475768\n",
      "<pad>: What=<pad>ATE TABLE table_name_1 (select__code_3,1ARCHAR, station_ARCHAR) question= What is the name number for the station<pad>?</s>\n",
      "799 7500 0.2625236511230469 0.0009648882429441257\n",
      "Human: context= CREATE TABLE table_name_2 (select_name_birth VARCHAR, place_ARCHAR) and V Vrank VARCHAR) question= What is the place of birth of the? \" 9, 2005:? and whenatial_ is \" V John Stien John.achia? Assistant:select place_of_birth FROM table_name_78 WHERE elevated = MAYmay 16, 1288\" AND cardinalatial_title = \"deacon of s. eustachio\"</s>\n",
      "899 7500 0.23500771820545197 0.0009524135262330098\n",
      "Human: context= CREATE TABLE table_name ( ( (8 (score VARCHAR, class V Vteam VARCHAR) question= What many college did a New team with?? Assistant:select collegeOUNT(n) FROM TABLE_2508633_11 WHERE nfl_team = \"BILLalo Bills\"</s>\n",
      "999 7500 0.2523467242717743 0.0009455032620941839\n",
      "Human: context= CREATE TABLE table_name_74 (score VARCHAR, rank Vmodel VARCHAR) question= What is is the vehicle with??une-??ou?dbs? Assistant:select STATUS from TABLE_NAME_55 where vehicleARCHICLE_TYPEPES = \"dBS\"TL\"DMCL\"</s>\n",
      "1099 7500 0.2681477665901184 0.0009303710135019718\n",
      "Human: context= CREATE TABLE table_name ( ( ( (8 (date_ARCHAR, year VARCHAR) question= What wasu released the album, is the album? Assistant:select albumBUM from TABLE_18442691_2 where ARTIST = \"AB\"</s>\n",
      "1199 7500 0.2544287145137787 0.0009221639627510075\n",
      "Human: context= CREATE TABLE table_name_53 (team VARCHAR, points VARCHAR) date VARCHAR) question= What building has aors of than 7? Building Building Building smaller less?s st Assistant:select LOCATION from TABLE_NAME_71 where FLOORS < 27 and BUILDING < 150150\"GIN\"</s>\n",
      "1299 7500 0.19381451606750488 0.0009045084971874737\n",
      "Human: context= CREATE TABLE table_name_1 (date VEGER, rank VARCHAR, dateuary_ARCHAR) question= What is the highest points of points for the opponent with jan?, the Golden Warriorsrie? Assistant:SELECT points(PO) FROM table_name_23 WHERE jan = \"Cveland barons\" AND january = \"</s>\n",
      "1399 7500 0.16163358092308044 0.0008950775061878452\n",
      "Human: context= CREATE TABLE table_name (__1 (dateonent VARCHAR, score VARCHAR, question= What was the play?? the 2013 that they with a-0 draw Assistant:SELECT opponent FROM table_13258806_2 WHERE record = \"2-2\"</s>\n",
      "1499 7500 0.1724175363779068 0.0008750555348152298\n",
      "Human: context= CREATE TABLE table_name_1 (date_team Vwin VARCHAR, home Vapps VARCHAR) question= What FA Cup apps has a apps? the- Assistant:SELECT min CupCUP_APPS from TABLE_NAME_28 where LEAGUE_APPS = 2121\"</s>\n",
      "1599 7500 0.11446956545114517 0.0008535533905932737\n",
      "Human: context= CREATE TABLE table_name_93 (date VEGER, name VARCHAR, question= What is the year number of the teamana alonents? Assistant:select avg(yearEAR) from TABLE_NAME_14 where TEAM = \"MONTREAL_OS\"</s>\n",
      "1699 7500 0.12963652610778809 0.0008422735529643444\n",
      "Human: context= CREATE TABLE table_name_93 (date VARCHAR, score VARCHAR) question= What Game had the genreer? Assistant:select GAME from TABLE_NAME_90 where GENRE = \"PLATFORMER\"</s>\n",
      "1799 7500 0.13657553493976593 0.0008187119948743449\n",
      "Human: context= CREATE TABLE table_name_45 (scoreetition VARCHAR, venue VARCHAR) question= What competition the competition when when the score is less?0? Assistant:SELECT COMP FROM table_name_44 WHERE score = 11-1\"</s>\n",
      "1899 7500 0.19178061187267303 0.0008064535268264883\n",
      "Human: context= CREATE TABLE table_name_6 (score V_ARCHAR, date_ARCHAR) date VARCHAR) question= What is has the for the county county was September/21/18? the county of Jeffersonaf? Assistant:SELECT PRO FROM table_name_65 WHERE listed = \"12/16/1994\" AND county = \"beaufort\"</s>\n",
      "1999 7500 0.1704811155796051 0.0007810416889260653\n",
      "Human: context= CREATE TABLE table_name_88 (score VARCHAR, team_titleest V Vnumber_ Vs_sapar_nameARCHAR) question= What is the'sdeva position when Assistant:SELECT POSITION from TABLE_NAME_60 where CO_CONTESTANT__YAAR_VS_PYAAR_ = \"Tina sACHDE\"</s>\n"
     ]
    }
   ],
   "source": [
    "for i, data in enumerate(loader):\n",
    "    with accelerator.accumulate(model_actor):\n",
    "        out = model_actor(**data)\n",
    "        accelerator.backward(out.loss)\n",
    "\n",
    "        if accelerator.sync_gradients:\n",
    "            accelerator.clip_grad_norm_(\n",
    "                [i for i in model_actor.parameters() if i.requires_grad], 1.0)\n",
    "\n",
    "        optimizer.step()\n",
    "        scheduler.step()\n",
    "        optimizer.zero_grad()\n",
    "\n",
    "    if (i + 1) % 100 == 0:\n",
    "        lr = optimizer.param_groups[0]['lr']\n",
    "        print(i, len(loader), out.loss.item(), lr)\n",
    "\n",
    "        logits = out.logits[0].argmax(1)\n",
    "        print(tokenizer.decode(logits))\n",
    "\n",
    "    if i == 2000:\n",
    "        break\n",
    "\n",
    "lora.merge(model_actor)\n",
    "model_actor.save_pretrained('model/actor')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:cuda117]",
   "language": "python",
   "name": "conda-env-cuda117-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
